package com.example.myapplication01.jetpack.room;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication01.R;

public class RoomActivity extends AppCompatActivity {

    UserViewModel viewModel;
    TextView show;
    Button insertBtn;
    RecyclerView recyclerView;
    MyAdapter mAdapter;


    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_room);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.room), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        insertBtn = findViewById(R.id.btn_insert);
        recyclerView = findViewById(R.id.recycleView);
        mAdapter = new MyAdapter();
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        viewModel = new ViewModelProvider(this).get(UserViewModel.class);
        viewModel.getLiveUserListData().observe(this, users -> {
            mAdapter.setList(users);
            mAdapter.notifyDataSetChanged();
        });


        insertBtn.setOnClickListener(v -> {
            viewModel.insert(new User("lisi", 23));
        });
    }


}